﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Testicide.Models;
using Testicide.DAL;
using System.Threading;
using WatiN.Core;

namespace Testicide.Controllers
{
    public class FeatureController : Controller
    {
        private TesticideContext db = new TesticideContext();

        //
        // GET: /Feature/

        public ActionResult Index(int? id)
        {
            if (id == null && TempData["mid"] != null)
            {
                int modID = (int)TempData["mid"];
                var feature = from m in db.Feature where m.ModuleID == modID select m;
                ViewBag.Message = "yes";
                ViewBag.ModuleName = db.Module.Find(modID).Name;
                ViewBag.ModuleID = modID;
                return View(feature.ToList());
            }
            else if (id != null)
            {
                var feature = from m in db.Feature where m.ModuleID == id select m;
                ViewBag.Message = "yes";
                ViewBag.ModuleName = db.Module.Find(id).Name;
                ViewBag.ModuleID = id;
                return View(feature.ToList());
            }
            ViewBag.Message = "no";
            return View(db.Feature.ToList());
            
        }

        //
        // GET: /Feature/Details/5

        public ActionResult Details(int id = 0)
        {
            Feature feature = db.Feature.Find(id);
            if (feature == null)
            {
                return HttpNotFound();
            }
            TempData["mID"] = feature.ModuleID;
            return RedirectToAction("Tool_box","Scenario");
        }

        //
        // GET: /Feature/Create

        public ActionResult Create(int id=0)
        {
            Feature feature = new Feature();
            feature.ModuleID = id;
            TempData["mID"] = feature.ModuleID;
            return View();
        }

        //
        // POST: /Feature/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Feature feature)
        {
            feature.Date = DateTime.Now;
            feature.ModuleID = (int)TempData["mID"];

            if (ModelState.IsValid)
            {
                db.Feature.Add(feature);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(feature);
        }

        //
        // GET: /Feature/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Feature feature = db.Feature.Find(id);
            if (feature == null)
            {
                return HttpNotFound();
            }
            TempData["mID"] = feature.ModuleID;
            ViewBag.ModuleID = new System.Web.Mvc.SelectList(db.Module, "ID", "Name", feature.ModuleID);
            return View(feature);
        }

        //
        // POST: /Feature/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Feature feature)
        {
            if (ModelState.IsValid)
            {
                db.Entry(feature).State = EntityState.Modified;
                db.SaveChanges();
                TempData["mID"] = feature.ModuleID;
                return RedirectToAction("Index");
            }
            ViewBag.ModuleID = new System.Web.Mvc.SelectList(db.Module, "ID", "Name", feature.ModuleID);
            return View(feature);
        }

        //
        // GET: /Feature/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Feature feature = db.Feature.Find(id);
            if (feature == null)
            {
                return HttpNotFound();
            }
            TempData["mID"] = feature.ModuleID;
            return View(feature);
        }

        //
        // POST: /Feature/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Feature feature = db.Feature.Find(id);
            List<Scenario> scenarios = feature.Scenarios.ToList();
            foreach(var item in scenarios)
            {
                db.Scenario.Remove(item);
            }
            db.Feature.Remove(feature);
            db.SaveChanges();
            TempData["mID"] = feature.ModuleID;
            return RedirectToAction("Index");
        }

        //
        //Search method
        //
        public ActionResult Search(string searchtxt)
        {
            var features = from p in db.Feature
                          select p;

            if (!String.IsNullOrEmpty(searchtxt))
            {
                features = features.Where(p => p.Name.ToUpper().Contains(searchtxt.ToUpper()));
            }
            if (features.Count() == 0)
            {
                ViewBag.Message = "The Feature You are searching for doesn't exist";
            }
            else
            {
                ViewBag.Message = "";
            }
            return View(features);
        }
        //
        //AutoComplete
        //
        public JsonResult AutoComplete(string term)
        {
            var result = db.Feature.Where(p => p.Name.ToLower().Contains(term.ToLower()))
                .Select(p => p.Name).ToList();

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult AddEditRecord(int? id, int modID)
        {
            if (Request.IsAjaxRequest())
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Feature feature = db.Feature.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("_FeatureData", feature);
                }
                ViewBag.IsUpdate = false;
                TempData["mid"] = modID;
                return PartialView("_FeatureData");
            }
            else
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Feature feature = db.Feature.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("FeatureData", feature);
                }
                ViewBag.IsUpdate = false;
                TempData["mid"] = modID;
                return View("FeatureData");
            }
        }

        [HttpPost]
        public ActionResult AddEditRecord(Feature feature, string cmd)
        {
            if (ModelState.IsValid)
            {
                if (cmd == "Save")
                {
                    feature.Date = DateTime.Now;
                    feature.ModuleID = (int)TempData["mid"];
                    db.Feature.Add(feature);
                    db.SaveChanges();
                    TempData["mid"] = feature.ModuleID;
                    return RedirectToAction("Index");
                }
                else
                {
                    Feature fet = db.Feature.Where(m => m.ID == feature.ID).FirstOrDefault();
                    if (fet != null)
                    {
                        fet.Name = feature.Name;
                        fet.Date = feature.Date;
                        fet.Status = feature.Status;
                        db.SaveChanges();
                    }
                    TempData["mid"] = fet.ModuleID;
                    return RedirectToAction("Index");
                }
            }
            TempData["mid"] = feature.ModuleID;
            return RedirectToAction("Index");
        }

        public ActionResult DeleteRecord(int id)
        {
            Feature feature = db.Feature.Find(id);
            List<Scenario> scenarios = feature.Scenarios.ToList();
            foreach (var item in scenarios)
            {
                db.Scenario.Remove(item);
            }
            db.Feature.Remove(feature);
            db.SaveChanges();
            TempData["mID"] = feature.ModuleID;
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}